<?php
namespace Admin\Model;
use Think\Model;
class AdminModel extends Model 
{
	protected $insertFields = array('username','password','cpassword','chkcode');
	protected $updateFields = array('id','username','password','cpassword');
	protected $_validate = array(
		array('username', 'require', '用户名不能为空！', 1, 'regex', 3),
		array('username', '1,30', '用户名的值最长不能超过 30 个字符！', 1, 'length', 3),
		array('password', 'require', '密码不能为空！', 1, 'regex', 1),
		//array('password', '1,32', '密码的值最长不能超过 32 个字符！', 1, 'length', 3),
	);
    public $_login_validate = array(
        array('username','require','用户名不能为空！',1),
        array('password','require','密码不能为空！',1),
        array('chkcode','require','验证码不能为空！',1),
        array('chkcode','check_verify','验证码错误！',1,'callback'),
    );
    /**验证验证码是否正确**/
    public function check_verify($code,$id=''){
        $verify = new \Think\Verify();
        return $verify->check($code,$id);
    }
    /**获取用户名密码**/
    public function login(){
        $username = $this->username;
        $password = $this->password;
        $user = $this->where(array(
            'username' => array('eq',$username),
        ))->find();
        if($user){
            if($user['password'] == md5($password)){
                session('id',$user['id']);
                session('username',$user['username']);
                return true;
            } else {
                $this->error = '密码不正确！';
                return false;
            }
        } else {
            $this->error = '用户名不存在！';
            return false;
        }
    }
	public function search($pageSize = 20)
	{
		/**************************************** 搜索 ****************************************/
		$where = array();
		if($username = I('get.username'))
			$where['username'] = array('like', "%$username%");
		if($password = I('get.password'))
			$where['password'] = array('like', "%$password%");
		/************************************* 翻页 ****************************************/
		$count = $this->alias('a')->where($where)->count();
		$page = new \Think\Page($count, $pageSize);
		// 配置翻页的样式
		$page->setConfig('prev', '上一页');
		$page->setConfig('next', '下一页');
		$data['page'] = $page->show();
		/************************************** 取数据 ******************************************/
        $data['data'] = $this->alias('a')
            ->field('a.*,b.role_id,b.admin_id,GROUP_CONCAT(c.role_name) role_name')
            ->join('LEFT JOIN __ADMIN_ROLE__ b ON a.id=b.admin_id
                    LEFT JOIN __ROLE__ c ON b.role_id=c.id')
            ->where($where)
            ->group('a.id')
            ->limit($page->firstRow.','.$page->listRows)
            ->select();
		return $data;
	}
	// 添加前
	protected function _before_insert(&$data, $option)
	{
	    $data['password'] = md5($data['password']);
	}
	// 修改前
	protected function _before_update(&$data, $option)
	{
	    if($data['password']){
	        $data['password'] = md5($data['password']);
        } else {
            unset($data['password']);
        }
        $arModel = D('admin_role');
        $arModel->where(array(
            'admin_id' => $option['where']['id'],
        ))->delete();

	}
	// 删除前
	protected function _before_delete($option)
	{
		$arModel = D('admin_role');
        $arModel->where(array(
            'admin_id' => $option['where']['id'],
        ))->delete();
	}
	//添加之后
    public function _after_insert($data,$option){
        $roleId = I('post.role_id');
        $arModel = D('admin_role');
        foreach ($roleId as $k=>$v){
            $arModel->add(array(
                'admin_id' => $data['id'],
                'role_id' => $v,
            ));
        }
    }
    public function _after_update($data,$option){
        $roleId = I('post.role_id');
        $arModel = D('admin_role');
        foreach ($roleId as $v){
            $arModel->add(array(
                'role_id' => $v,
                'admin_id' => $option['where']['id'],
            ));
        }

    }

    public function logout(){
        session(null);
    }
}